package com.hoperun.checkcloth.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.base.MIP_BaseActivity;
import com.hoperun.checkcloth.net.IServiceImpl;
import com.hoperun.checkcloth.net.RequestServiceImpl;
import com.hoperun.checkcloth.ui.dialog.SimpleDialog;
import com.hoperun.checkcloth.util.LanguageUtil;
import com.hoperun.checkcloth.util.MD5Encrypt;
import com.hoperun.checkcloth.util.SharedPreUtil;

import org.json.JSONObject;

public class JyLoginActivity extends MIP_BaseActivity implements View.OnClickListener {

    private Button loginBtn;
    private EditText accountEt;
    private EditText passwordEt;
    private Button languageBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jy_login);
        initDate();
        initView();
        initEvent();
    }

    private void initDate() {

    }

    private void initView() {
        loginBtn = findViewById(R.id.login_loginBtn);
        accountEt = findViewById(R.id.login_accountEt);
        passwordEt = findViewById(R.id.login_passwordEt);
        languageBtn = findViewById(R.id.languageBtn);
        accountEt.setFocusable(true);
        accountEt.setFocusableInTouchMode(true);

        accountEt.requestFocus();
        accountEt.requestFocusFromTouch();
        accountEt.findFocus();
    }

    private void initEvent() {
        loginBtn.setOnClickListener(this);
        accountEt.setOnKeyListener((v, keyCode, event) -> {
            if (event != null && event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                startLogin();
            }
            return false;
        });
        languageBtn.setOnClickListener(view -> {
            LanguageUtil.changeLanguage();

        });
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.login_loginBtn) {
            startLogin();
        }
    }

    private void startLogin() {
        String account = accountEt.getText().toString().trim();
        String pwd = passwordEt.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            SimpleDialog.Companion.newInstance(getString(R.string.login_error_account_empty)).show(getSupportFragmentManager(), "DialogTip");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            SimpleDialog.Companion.newInstance(getString(R.string.login_error_password_empty)).show(getSupportFragmentManager(), "DialogTip");
            return;
        }
        login(account,pwd);
    }

    /**
     * 登录
     */
    private void login(String account,String pwd) {
        showWait();
        RequestServiceImpl.jy_login(mMIPHandler, IServiceImpl.LOGIN_JIYAN, account, MD5Encrypt.MD5(pwd));
    }


    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) {

        stopWait();
        String msg = "";
        String opt = "";
            if (requestType == IServiceImpl.LOGIN_JIYAN) {
                try {
                    JSONObject jsonObj = new JSONObject((String) objBody);
                    msg = jsonObj.optString("MSG_INFO");
                    opt = jsonObj.optString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        JSONObject data = jsonObj.optJSONObject("DATA");
                        if (data != null) {

                            String YHBH = data.optString("YHBH");
                            String YHMC = data.optString("YHMC");
                            //String DATASUM = data.optString("DATASUM");
                            SharedPreUtil.putValue(this, SharedPreUtil.YHBH, YHBH);
                            SharedPreUtil.putValue(this, SharedPreUtil.YHMC, YHMC);
                           // SharedPreUtil.putValue(this, SharedPreUtil.DATASUM, DATASUM);
                            Intent intent = new Intent(JyLoginActivity.this, JyMainActivity.class);
                            startActivity(intent);

                        } else {
                            showSimpleDialog(getString(R.string.login_fail));
                        }

                    } else {
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog(getString(R.string.login_fail));
                        } else {
                            showSimpleDialog(msg);
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showSimpleDialog(getString(R.string.login_fail));
                }
            }
    }


    private void showSimpleDialog(String message) {
        SimpleDialog.Companion.newInstance(message).show(getSupportFragmentManager(), "DialogTip");
    }

}
